package cn.jinzhu.provider.controller;

import cn.jinzhu.IUserService;
import cn.jinzhu.entity.User;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class HelloController implements IUserService {
     @Value("${server.port}")
     Integer port;
    @Override
    @GetMapping("/hello")
    @RateLimiter(name ="rlA")//resilience4J 里面的三大主流之限流，rlA就是限流名称
    public String hello(){
        System.out.println(new Date());
        return "你好Cloud"+port;
    }

    @Override
   // @GetMapping("/hello1")
    public String hello1(String name){
        System.out.println("嗯嗯");
        return  "你好"+name+new Date();
    }


    @PostMapping("/add1")
    public String hello2( User user){
        return user.getName()+user.getPass()+user.getId();
    }
    @Override
   // @PostMapping("/add2")
    public User hello3(@RequestBody User user){
        return user;
    }

    @PutMapping("/put1")
    public String put1(@RequestBody User user){
        System.out.println(user.getName());
        return  "更新了"+user.getName();
    }


    @Override
   // @DeleteMapping("/delete1/{id}")
    public String delete1(@PathVariable Integer id ){
        System.out.println("删除了");
        return  "更新了"+id;
    }

    @DeleteMapping("/delete2")
    public String delete2( Integer id ){
        System.out.println("删除了");
        return  "更新了"+id;
    }
    @GetMapping("/userById/{ids}")//合并之后的请求
    public List<User> userById(@PathVariable  String ids){
        String[] split = ids.split(",");
        List<User>  user = new ArrayList<>();
        for (String s:split
             ) {
            User user1 =new User();
            user1.setId(Integer.parseInt(s));
            user.add(user1);
        }
        return  user;
    }





    //openfeign
    @Override
    //@GetMapping("/user1")
    public String GetUserByName(@RequestHeader String name) throws UnsupportedEncodingException {
        System.out.println(URLEncoder.encode(name,"utf-8"));
        return URLEncoder.encode(name,"utf-8");
    }
}
